<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    const debounce = function (cb, wait) {
      let timeout = null
      return function () {
        let _this = this
        let args = arguments

        clearSetTimeout(timeout)
        timeout = setTimeout(function () {
          cb.apply(_this, args)
        }, wait)
      }
    }

    const debounceImmediate = function (cb, wait, immediate) {
      let timeout = null
      return function () {
        let _this = this
        let args = arguments

        if (timeout) clearSetTimeout(timeout)
        if (immediate) {
          let isImmediate = !timeout
          timeout = setTimeout(function () {
            timeout = null
          }, wait)
          if (isImmediate) cb.apply(_this, args)
        } else {
          timeout = setTimeout(function () {
            cb.apply(_this, args)
          }, wait)
        }
      }
    }

    function throttle(cb, daly) {
      let timer = null
      return function (...args) {
        let _this = this
        if (!timer) {
          timer = setTimeout(function () {
            cb.apply(_this, args)
            timer = null
          }, dlay)
        }
      }
    }
  </script>
</html>
